using System.Collections;
using System.Collections.Generic;

/// <summary>
/// 该类由shaco.Execel+Serializable.cs脚本自动生成，请勿手动修改
/// </summary>
namespace ShootMan
{
	public class cfg_localization : shaco.Base.IExcelData
	{
		public class RowData 
		{
			public System.String KEY { get { return _KEY; } }
			private System.String _KEY;
			public System.String CN { get { return _CN; } }
			private System.String _CN;
			public System.String EN { get { return _EN; } }
			private System.String _EN;

			public RowData(System.String KEY, 
							System.String CN, 
							System.String EN)
			{
				this._KEY = KEY;
				this._CN = CN;
				this._EN = EN;
			}

			public RowData() { }

			public RowData Clone()
			{
				var cloneData = new RowData();
				cloneData._KEY = this.KEY;
				cloneData._CN = this.CN;
				cloneData._EN = this.EN;

				return cloneData;
			}
		}

		static public int Count { get {return shaco.Base.ExcelHelper.GetExcelData<cfg_localization>()._rowDatas.Count;} }
		static public cfg_localization Instance { get {return shaco.Base.ExcelHelper.GetExcelData<cfg_localization>();} }
		
		private List<RowData> _rowDatas = new List<RowData>();

		static public RowData Get(System.Int32 index)
		{
			var data = CheckInit();
			if (index < 0 || index > data._rowDatas.Count - 1)
			{
				shaco.Base.Log.Error("cfg_localization Get error: out of range, index=" + index + " count=" + data._rowDatas.Count);
				return null;
			}
			return data._rowDatas[index];
		}

		static public bool ContainsKey(System.Int32 index)
		{
            var data = CheckInit();
			return index >= 0 && index < data._rowDatas.Count;
        }

		static public void Foreach(System.Func<RowData, bool> callback)
		{
			var data = CheckInit();
            using (var en = data.GetEnumerator())
			{
				while (en.MoveNext())
				{
                    if (!callback(en.Current))
						break;
				}
			}
		}

		public IEnumerator<RowData> GetEnumerator()
		{
			if (0 == _rowDatas.Count) UpdateData();
            using (var en = _rowDatas.GetEnumerator())
			{
				while (en.MoveNext())
				{
					yield return en.Current;
				}
			}
		}

		public void CheckInitAsync(System.Action<float> callbackProgress = null)
		{
            if (0 < _rowDatas.Count)
			{
				if (null != callbackProgress)
					callbackProgress(1.0f);
				return;
			}

            shaco.ExcelHelper.OpenAsync("Assets/ShootMan/ConfigAsset/cfg_localization.asset", (excelData) =>
			{
				if (null == excelData)
				{
					if (null != callbackProgress)
						callbackProgress(1.0f);
					return;
				}
				
				int rows = excelData.GetTabelDataRowCount();
				int index = 0;

#if UNITY_WEBGL
				shaco.Base.Coroutine.ForeachCount(rows,
#else
                shaco.Base.Coroutine.ForeachCountAsync(rows, 
#endif
				() =>
				{
                    this.UpdateDataOnce(excelData, index++);
					return true;
				}, (percent2) =>
				{
                    if (null != callbackProgress)
					{
                        callbackProgress(percent2 * 0.7f + 0.3f);
					}
#if UNITY_WEBGL
				}, shaco.ExcelHelper.DEFAULT_WEBGL_LOAD_ASYNC_SPEED);
#else
				});
#endif
                }, (percent) =>
			{
				if (null != callbackProgress)
                    callbackProgress(percent * 0.3f);
			});
        }
				
		private void UpdateData()
		{
			var excelData = shaco.ExcelHelper.Open("Assets/ShootMan/ConfigAsset/cfg_localization.asset");
			if (null != excelData)
			{
				int rows = excelData.GetTabelDataRowCount();
				for (int i = 0; i < rows; ++i)
				{
                    UpdateDataOnce(excelData, i);
				}
                shaco.ExcelHelper.Close(excelData);
			}
		}

		private void UpdateDataOnce(shaco.Base.ExcelData excelData, int i)
		{
            if (!excelData.IsIgnoreRowData(i))
			{
				RowData newData = null;
				try
				{
					var v0 = excelData.GetData(i, 0);
					var v1 = excelData.GetData(i, 1);
					var v2 = excelData.GetData(i, 2);

					newData = new RowData
					(
						v0,v1,v2
					);
					_rowDatas.Add(newData);
				}
				catch (System.Exception e)
				{
					shaco.Base.Log.Error("[cfg_localization] UpdateDataOnce error: index=" + i + " e=" + e);
				}
			}
		}

		static private cfg_localization CheckInit()
		{
            var retValue = shaco.Base.ExcelHelper.GetExcelData<cfg_localization>();
            if (0 == retValue._rowDatas.Count) 
				retValue.UpdateData();
			return retValue;
		}
	}
}